﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using Helpers;
using Interfaces;

namespace xLibris
{
    public class MenuElement : NotifyPropertyChangedBase
    {
        public MenuViewModel Owner { get; set; }
        public MenuElement Parent { get; set; }
        public string Name { get; set; }
        public string IconFilename { get; set; }
        public List<MenuElement> Children { get; set; }
        public Lazy<IModule, IModuleMetadata> App { get; set;  }

        public MenuElement()
        {
        }

        ImageSource _icon;
        public ImageSource Icon
        {
            get
            {
                if (_icon == null)
                    _icon = LoadIcon();
                return _icon;
            }
            set { _icon = value; }
        }

        public Visibility AppSign
        {
            get
            {
                if (Children == null)
                    return Visibility.Visible;
                return Visibility.Hidden;
            }
        }

        public BitmapImage LoadIcon()
        {
            if (File.Exists(@"Images\" + IconFilename))
            {
                BitmapImage image = new BitmapImage();

                try
                {
                    image.BeginInit();
                    image.CacheOption = BitmapCacheOption.OnLoad;
                    image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    image.UriSource = new Uri(@"Images\" + IconFilename, UriKind.RelativeOrAbsolute);
                    image.EndInit();
                }
                catch
                {
                    return null;
                }

                return image;
            }
            return null;
        }

        public MenuElement ParseMenu(MenuViewModel owner, MenuElement p, XElement node)
        {
            Owner = owner;
            Parent = p;
            if (node.Element("name") != null)
                Name = node.Element("name").Value;
            if (node.Element("icon") != null)
                IconFilename = node.Element("icon").Value;
            Children = node.Elements("group").Select(x => new MenuElement().ParseMenu(owner,this, x)).ToList();
            return this;
        }

        RelayCommand _executeCommand;
        public ICommand ExecuteCommand
        {
            get
            {
                if (_executeCommand == null)
                {
                    _executeCommand = new RelayCommand(
                        param => this.Exec(),
                        param => this.CanExec);
                }
                return _executeCommand;
            }
        }

        void Exec()
        {

            if (Children != null)
                Owner.CurrentMenu = this;
            else
            {
                var a = App;
                var v = a.Value.View();
                Owner.AppView = v;
            }
        }

        public bool CanExec { get { return true; } }

    }
}
